package com.xjoyt.pay.gateway.aop;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xjoyt.pay.gateway.PayGatewayApp;
import com.xjoyt.pay.gateway.constants.RedisConstant;
import com.xjoyt.pay.gateway.vo.TokenSession;
import com.xjoyt.pay.master.po.Distributor;
import com.xjoyt.pay.master.po.Merchant;

public abstract class AbstractAspect {

    protected static final Logger logger = LoggerFactory
            .getLogger(AbstractAspect.class);

    protected StringRedisTemplate getRedisTemplate() {
        return PayGatewayApp.applicationContext.getBean(StringRedisTemplate.class);
    }

    /**
     * 获取代理商
     * 
     * @param accessToken
     * @return
     */
    protected Distributor getDistributor(String accessToken) {
        if (StringUtils.isBlank(accessToken)) {
            return null;
        }
        String value = getRedisTemplate().opsForValue()
                .get(RedisConstant.REDIS_KEY_ACCESS_TOKEN + accessToken);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        Gson gson = new Gson();
        @SuppressWarnings("unchecked")
        TokenSession<Map<String, Object>> token = gson.fromJson(value,
                TokenSession.class);
        if (token == null || token.auth == null || token.type != 1) {
            return null;
        }
        String json = gson.toJson(token.auth);
        return gson.fromJson(json, Distributor.class);
    }

    /**
     * 获取商户
     * 
     * @param accessToken
     * @return
     */
    protected Merchant getMerchant(String accessToken) {
        if (StringUtils.isBlank(accessToken)) {
            return null;
        }
        String value = getRedisTemplate().opsForValue()
                .get(RedisConstant.REDIS_KEY_ACCESS_TOKEN + accessToken);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        Gson gson = new Gson();
        @SuppressWarnings("unchecked")
        TokenSession<Map<String, Object>> token = gson.fromJson(value,
                TokenSession.class);
        if (token == null || token.auth == null || token.type != 0) {
            return null;
        }
        String json = gson.toJson(token.auth);
        return gson.fromJson(json, Merchant.class);
    }

    protected String toString(Object src) {
        if (src == null) {
            return null;
        }
        GsonBuilder builder = new GsonBuilder()
                .setExclusionStrategies(new ExclusionStrategy() {

                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return f.getAnnotation(JsonIgnore.class) != null;
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                });
        Gson gson = builder.create();
        return gson.toJson(src);
    }

    protected ServletRequestAttributes getServletRequestAttributes() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return attrs;
    }

    protected HttpServletRequest getHttpServletRequest() {
        return getServletRequestAttributes().getRequest();
    }
}
